#include "reduction_list.h"

void init_reduction_list(reduction_list *list)
{
    list->list = (uint32_t*) malloc(sizeof(uint32_t)*LIST_ALLOC_SIZE);
    list->ceil = LIST_ALLOC_SIZE;
    list->tol = 0;
#if TEST
    list->max = 0;
    list->reallocations = 0;
#endif
}

void append_position_on_reduction_list(reduction_list *list, uint32_t node)
{
    if (list->tol >= list->ceil) {
        list->list = (uint32_t*) realloc(list->list, sizeof(uint32_t)*(list->ceil + LIST_ALLOC_SIZE));
        list->ceil += LIST_ALLOC_SIZE;
#if TEST
        ++list->reallocations;
#endif
    }
    list->list[list->tol] = node;
    ++list->tol;
#if TEST
    if (list->tol > list->max) {
        list->max = list->tol;
    }
#endif
}

uint32_t get_reduction_position_at_index(reduction_list *list, uint32_t index)
{
    if (index >= list->tol) {
        return 0;
    }
    return list->list[index];
}

void swap_reduction_lists(reduction_list **src, reduction_list **dst)
{
    reduction_list *tmp = *src;
#if TEST
    (*dst)->max = (*src)->max;
    (*dst)->reallocations = (*src)->reallocations;
#endif
    *src = *dst;
    *dst = tmp;
    (*src)->tol = 0;
}

void free_reduction_list(reduction_list *list)
{
    free(list->list);
    free(list);
}

